package com.leyou.auth.web;

import com.leyou.auth.service.UserAuthService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserAuthController {

    private final UserAuthService userAuthService;

    public UserAuthController(UserAuthService userAuthService) {
        this.userAuthService = userAuthService;
    }


    /**
     * 用户登录授权
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<Void> userLogin(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            HttpServletResponse response) {

        this.userAuthService.userLogin(username,password,response);

        return ResponseEntity.ok().build();
    }

    /**
     * 退出登录，删除redis中的jti，删除浏览器本地的cookie，由于本地cookie设置了httpOnly，所以没有办法在前端删除
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/logout")
    public ResponseEntity<Void> userLogout(HttpServletRequest request,HttpServletResponse response){

        this.userAuthService.userLogout(request,response);

        return ResponseEntity.ok().build();
    }
}